<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="520px"
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改组织信息' : '添加组织信息'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="92px">
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="上级组织:">
             <el-input v-model="form.parentName" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="组织类型:" prop="type">
            <el-radio-group v-model="form.type">                       
              <el-radio-button v-for="(item) in orgTypes" :key="item.Key" :label="item.Key">{{ item.Value }}</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="组织代码:" >
            <el-input  v-model="form.code" placeholder="系统自动生成"  />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="组织名称:" prop="name">
            <el-input  v-model="form.name" placeholder="请输入组织名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="备注:" >
            <el-input  v-model="form.remark" placeholder="请输入备注" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save"   v-permission="'Basic_Org_Save'"  >
        保存
      </el-button>
    </div>
  </ele-modal>
</template>

<script>
  import { QueryOrganiztionTypeEnum } from '@/api/enummanger';
  import { UpdateOrganizational,AddOrganizational,QueryOrganiztionById } from '@/api/usermanager/organizationmanager';
  const DEFAULT_FORM = {
    id: '',
    name: '',
    code:'',
    remark:'',
    type:'',
    oLevel: '',
    parentId: '',
    parentName :'',
    createDate:'',
    createUser:'',
    createUserName:'',
  };

  export default {
    name: 'OrganizationEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    data() {
      return {
        // 表单数据
        form: { ...DEFAULT_FORM },
        // 表单验证规则
        rules: {
          type: [{required: true, message: '请输入组织类型',trigger: 'blur'}],
          name: [{required: true, message: '请输入组织名称',trigger: 'blur'}],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        // 组织类型
        orgTypes:[],
      };
    },

    //初始化数据
    created(){
      this.initData();
    },
    methods: {

      /* 系统状态 */
      async initData(){
        let sysEnums = await QueryOrganiztionTypeEnum();
        sysEnums.map(e=>{
          if(e.Key!=1){
            this.orgTypes.push({Key:e.Key,Value:e.Value});
          }
        });
      },

      //页面赋值
      setData(data){
        QueryOrganiztionById(data.id)
        .then((res) => {
          this.$util.assignObject(this.form, {...res});
          //父节点赋值
          this.form.parentId = this.data.parentId;
          this.form.parentName = this.data.parentName;
        })
        .catch((e) => {
          this.$util.assignObject(this.form, {...this.data});
        });
      },

      /* 保存编辑 */
      save() {
        //校验必填信息
        this.$refs['form'].validate((valid) => {
          if (!valid) {return false;}
          
          const saveOrUpdate = this.isUpdate ? UpdateOrganizational : AddOrganizational;

          this.loading = true;
          saveOrUpdate(this.form)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              //回调父组件方法
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
      },
    },
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data && this.data.id) {
            this.setData(this.data);
            this.isUpdate = this.data.id !=null && this.data.id !=undefined;
          } else {
            this.isUpdate = false;
            this.form = { ...DEFAULT_FORM };
            this.form.oLevel = this.data.oLevel;
            this.form.parentId = this.data.parentId;
            this.form.parentName = this.data.parentName;
          }
        } else {
          //移除校验结果并重置字段
          // this.$refs['form'].resetFields();
          //移除校验结果
          this.$refs['form'].clearValidate();
          this.form = { ...DEFAULT_FORM };
        }
      }
    }
  };
</script>
